Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to call for shutting down the ResourceManager thread pool #815

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Malkierian
Copy link
Contributor

This adds a method in ResourceManager to allow for setting up a thread pool shutdown to prevent crashes when exiting while still loading assets. I have a wait time of 2 seconds here between pausing and purging to prevent deadlocks, more than that is unnecessary (especially if it's only for resource threads, as I think it should be). Unfortunately, because of the way shutdowns work, this has to be called manually from the port before nulling Context. This is demonstrated in the asset preload PR: HarbourMasters/Shipwright#4652

The reasoning for this PR is that I needed to be able to shut down the resource thread pool because of the way preloading would queue up a lot of loads at the start, and would crash if you tried to exit while in the opening sequence. This does not do any of the work of exposing the ResourceManager thread pool because there's still some ambiguity around the scope of that work (should only be used for non-critical tasks in my opinion; no save or config writing). The reason I needed to do it on the preload PR is because I couldn't get the crash to happen on latest develop. It's easiest to recreate with the 4K Reloaded pack. To let it trigger, comment the line OTRGlobals::Instance->context->GetResourceManager()->ShutDownThreadPool(); in OTRGlobals (should be line 1248), and close the program as soon as you see the Nintendo logo. Uncomment it to see it not crash under the same circumstances. I think it still happens in release mode, but you'll get the stack trace in debug in VS.

@Malkierian Malkierian force-pushed the shut-down-resource-thread-pool branch from 9bf449d to 43cd96f Compare February 10, 2025 07:11
@@ -410,4 +410,10 @@ void ResourceManager::SetAltAssetsEnabled(bool isEnabled) {
mAltAssetsEnabled = isEnabled;
}

void ResourceManager::ShutDownThreadPool() {
mThreadPool->pause();
mThreadPool->wait_for(std::chrono::duration<double>(2));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see you mentioned

I have a wait time of 2 seconds here between pausing and purging to prevent deadlocks

that feels a little bit magic to me, are we just assuming we'll never have a pool where 2 seconds isn't enough time? have you looked into alternative ways to mitigate deadlock that don't rely on a hardcoded delay?

Copy link
Collaborator

@briaguya-ai briaguya-ai Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kenix3's suggestion is to just pass the amount of time to wait as a function param

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants